package org.ourgrid.worker.business.requester;

import java.util.ArrayList;
import java.util.List;

import org.ourgrid.common.interfaces.to.WorkerStatus;
import org.ourgrid.common.internal.IResponseTO;
import org.ourgrid.common.internal.RequesterIF;
import org.ourgrid.common.internal.response.LoggerResponseTO;
import org.ourgrid.worker.business.controller.ExecutionController;
import org.ourgrid.worker.business.controller.WorkerController;
import org.ourgrid.worker.business.dao.WorkerDAOFactory;
import org.ourgrid.worker.business.dao.WorkerStatusDAO;
import org.ourgrid.worker.business.messages.ControlMessages;
import org.ourgrid.worker.business.messages.WorkerManagementControllerMessages;
import org.ourgrid.worker.request.StopWorkingRequestTO;
import org.ourgrid.worker.response.StatusChangedResponseTO;

public class StopWorkingRequester implements RequesterIF<StopWorkingRequestTO> {

	public List<IResponseTO> execute(StopWorkingRequestTO request) {
		List<IResponseTO> responses = new ArrayList<IResponseTO>();
		
		String senderPubKey = request.getSenderPublicKey();
		
		if (!senderPubKey.equals(request.getPeerPublicKey())) {
			responses.add(new LoggerResponseTO(
					WorkerManagementControllerMessages.getStopWorkingByUnknownPeerMessage(senderPubKey),
					LoggerResponseTO.WARN));
			
			return responses;
		}
		
		WorkerStatusDAO workerStatusDAO = WorkerDAOFactory.getInstance().getWorkerStatusDAO();
		
		String masterPeerAddress = workerStatusDAO.getMasterPeerAddress();
		
		if (masterPeerAddress == null) {
			responses.add(new LoggerResponseTO(
					WorkerManagementControllerMessages.getMasterPeerTryingToCommandWorkerBeforeSettingAsManagerMessage(), 
					LoggerResponseTO.WARN));
			
			return responses;
		}
		
		if (workerStatusDAO.isErrorState()) {
			StatusChangedResponseTO statusChangedResponseTO = new StatusChangedResponseTO();
			statusChangedResponseTO.setClientAddress(masterPeerAddress);
			statusChangedResponseTO.setStatus(workerStatusDAO.getStatus());
			responses.add(statusChangedResponseTO);

			responses.add(new LoggerResponseTO(
					WorkerManagementControllerMessages.getMasterPeerTryingToCommandWorkerOnErrorStateMessage(), 
					LoggerResponseTO.WARN));

			return responses;
		}

		if (!workerStatusDAO.isAllocated()) {
			responses.add(new LoggerResponseTO(WorkerManagementControllerMessages.getStopWorkingOnNotWorkingWorkerMessage(), 
					LoggerResponseTO.WARN));

			return responses;
		}

		boolean isWorkingState = workerStatusDAO.isWorkingState();

		WorkerController.getInstance().interruptWorking(responses, true);

		responses.add(new LoggerResponseTO(WorkerManagementControllerMessages.getSuccessfulStopWorkingMessage(), 
				LoggerResponseTO.INFO));

		WorkerStatus oldStatus = workerStatusDAO.getStatus();
		WorkerStatus newStatus = WorkerStatus.IDLE;
		workerStatusDAO.setStatus(newStatus);

		if (workerStatusDAO.isPreparingAllocationState()) {
			return responses;
		}
		
		if (isWorkingState) {
			ExecutionController.getInstance().beginAllocation(responses);
		} else {
			
			responses.add(new LoggerResponseTO(ControlMessages.getWorkerStatusChangedMessage(oldStatus, newStatus), 
					LoggerResponseTO.DEBUG));
			
			StatusChangedResponseTO to = new StatusChangedResponseTO();
			to.setClientAddress(masterPeerAddress);
			to.setStatus(newStatus);
			
			responses.add(to);
		}

		return responses;
	}
	
}
